package com.example.tiptravel.view.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CircleOptions;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.example.tiptravel.R;
import com.example.tiptravel.commom.base.BaseActivity;
import com.example.tiptravel.commom.utils.ImageLoader;
import com.example.tiptravel.entity.home.NearBy;
import com.example.tiptravel.entity.home.ScenicsBean;
import com.example.tiptravel.presenter.home.NearByPresenter;
import com.example.tiptravel.view.adapter.MapAdapter;
import com.example.tiptravel.view.adapter.MapVpAdapter;
import com.example.tiptravel.view.impl.home.NearbyView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class MapActivity extends BaseActivity implements NearbyView {


    @BindView(R.id.iv_map_back)
    ImageView ivMapBack;
    @BindView(R.id.et_map_search)
    EditText etMapSearch;
    @BindView(R.id.iv_map_menu)
    ImageView ivMapMenu;
    @BindView(R.id.mv_map)
    MapView mvMap;
    @BindView(R.id.rclv_map)
    RecyclerView rclvMap;

    @Override
    protected int getLayout() {
        return R.layout.activity_map;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        client.startLocation();
        mvMap.onCreate(savedInstanceState);
        initVp();
    }

    /**
     * 初始化ViewPager
     */
    private List<ScenicsBean> data = new ArrayList<>();
    private MapAdapter mapAdapter;
    private void initVp() {
        rclvMap.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        mapAdapter = new MapAdapter(data,this);
        rclvMap.setAdapter(mapAdapter);
        rclvMap.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    /**
     * 初始化地图
     */
    private AMap aMap;

    private void initMap(LatLng latLng) {
        aMap = mvMap.getMap();
        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(true);
        CameraUpdate camerupdata = CameraUpdateFactory.newLatLngZoom(latLng,7.5f);
        aMap.moveCamera(camerupdata);
        MarkerOptions options = new MarkerOptions();
        options.position(latLng);
        options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.amap_my_location));
        aMap.addMarker(options);
    }


    @OnClick({R.id.iv_map_back, R.id.et_map_search, R.id.iv_map_menu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_map_back:
                finish();
                break;
            case R.id.et_map_search:

                break;
            case R.id.iv_map_menu:
                Intent intent = new Intent(this, SpotActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mvMap.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mvMap.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        client.stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mvMap.onDestroy();
        client.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mvMap.onSaveInstanceState(outState);
    }

    @Override
    public void onSuccess(NearBy nearBy) {
        if (nearBy!=null&&nearBy.getStatus().equals("1")){
            List<ScenicsBean> list = nearBy.getData().getScenics();
            for (int i = 0; i < list.size(); i++) {
                MarkerOptions options = new MarkerOptions();
                options.icon(BitmapDescriptorFactory.fromResource(R.mipmap.amap_point_default));
                LatLng latLng = new LatLng(Double.valueOf(list.get(i).getGeo().getLat()), Double.valueOf(list.get(i).getGeo().getLng()));
                options.position(latLng);
                options.title("123");
                options.snippet("123");
                aMap.addMarker(options);
            }
            data.addAll(list);
            mapAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailure(Throwable e) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation!=null&&aMapLocation.getErrorCode()==0){
            NearByPresenter presenter = new NearByPresenter(this);
            Map<String, String> map = new HashMap<>();
            map.put("version", "android_3.8.1");
            map.put("lat", String.valueOf(aMapLocation.getLatitude()));
            map.put("lng", String.valueOf(aMapLocation.getLongitude()));
            presenter.getNearByData(map);
            LatLng latLng = new LatLng(aMapLocation.getLatitude(),aMapLocation.getLongitude());
            initMap(latLng);
        }
    }
}
